package com.tmc.dao.impl.intair;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.tmc.bean.common.ContextInfo;
import com.tmc.bean.common.Pagination;
import com.tmc.dao.impl.BaseDaoImpl;
import com.tmc.entity.intair.IntairOrder;
import com.tmc.util.DateUtil;
import com.tmc.util.ObjectUtil;

@Repository
public class IntairOrderDaoImpl extends BaseDaoImpl<IntairOrder> {

	public Pagination<IntairOrder> findByCon(ContextInfo context, IntairOrder cond, int pageIndex, int pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from IntairOrder c where c.enabledFlag = 'Y' ");
		if (ObjectUtil.hasText(cond.getOrderNum())) {
			sql.append(" and c.orderNum like :orderNum ");
			params.put("orderNum", "%" + cond.getOrderNum() + "%");
		}
		if (ObjectUtil.hasText(cond.getPlatformOrderNum())) {
			sql.append(" and c.platformOrderNum like :platformOrderNum ");
			params.put("platformOrderNum", "%" + cond.getPlatformOrderNum() + "%");
		}
		if (ObjectUtil.hasText(cond.getPassengerNames())) {
			sql.append(" and c.passengerNames like :passengerNames ");
			params.put("passengerNames", "%" + cond.getPassengerNames() + "%");
		}
		if (ObjectUtil.hasText(cond.getBookDateStartStr())) {
			sql.append(" and c.bookDate >= :bookDateStartStr ");
			params.put("bookDateStartStr", DateUtil.toLongDate(cond.getBookDateStartStr(), "yyyy-MM-dd"));
		}
		if (ObjectUtil.hasText(cond.getBookDateEndStr())) {
			sql.append(" and c.bookDate <= :bookDateEndStr ");
			params.put("bookDateEndStr", DateUtil.toLongDate(cond.getBookDateEndStr(), "yyyy-MM-dd"));
		}
		if (ObjectUtil.hasText(cond.getPnr())) {
			sql.append(" and c.pnr like :pnr ");
			params.put("pnr", "%"+cond.getPnr()+"%");
		}
		if (ObjectUtil.hasText(cond.getFlightNo())) {
			sql.append(" and c.flightNo like :flightNo ");
			params.put("flightNo", "%"+cond.getFlightNo()+"%");
		}
		if (ObjectUtil.hasText(cond.getOrderStatus())) {
			sql.append(" and c.orderStatus = :orderStatus ");
			params.put("orderStatus", cond.getOrderStatus());
		}
		if (ObjectUtil.hasText(cond.getPlatform())) {
			sql.append(" and c.platform = :platform ");
			params.put("platform", cond.getPlatform());
		}
		
		if (cond.getBookId() != null) {
			sql.append(" and c.createdBy = :bookId ");
			params.put("bookId", cond.getBookId());
		}
		
		sql.append(" order by c.lastUpdateDate desc ");
		return this.findPagination(sql.toString(), params, pageIndex, pageSize);
	}
	
	public List<IntairOrder> findByYesterday(ContextInfo context){
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from IntairOrder c where c.enabledFlag = 'Y' ");
		sql.append(" and c.lastUpdateDate >= :yesterdayStart");
		sql.append(" and c.lastUpdateDate < :todayStart");
		Long todayStart = DateUtil.toLongDate(// 截取为当天  *000000 时刻
				DateUtil.toStrDate(new Date(), "yyyy-MM-dd"), "yyyy-MM-dd");
		params.put("yesterdayStart", todayStart - DateUtil.Day2Long);
		params.put("todayStart", todayStart);
		
 		return this.findList(sql.toString(), params);
	}

	public List<IntairOrder> findAll(ContextInfo context) {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from IntairOrder c where c.enabledFlag = 'Y' ");
		return this.findList(sql.toString(), params);
	}

	public IntairOrder getByOrderNum(ContextInfo context, String orderNum) {
		if (!ObjectUtil.hasText(orderNum)) {
			return null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from IntairOrder c where c.enabledFlag = 'Y' ");
		sql.append(" and c.orderNum = :orderNum ");
		params.put("orderNum", orderNum);
		return this.get(sql.toString(), params);
	}
	
	public IntairOrder getByPlatformOrderNum(ContextInfo context, String platform, String orderNum) {
		if (!ObjectUtil.hasText(orderNum)) {
			return null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from IntairOrder c where c.enabledFlag = 'Y' ");
		sql.append(" and c.platform = :platform ");
		sql.append(" and c.platformOrderNum = :orderNum ");
		params.put("platform", platform);
		params.put("orderNum", orderNum);
		return this.get(sql.toString(), params);
	}

	public Long getTodayOrdersCount(ContextInfo context, String tag) {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" select count(*) from IntairOrder c where orderNum like :tag");
		sql.append(" and c.orderNum like :tag ");
		params.put("tag", tag + "%");
		return this.get(sql.toString(), params);
	}
	
	public IntairOrder getCurrent(ContextInfo context) {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from IntairOrder c where c.enabledFlag = 'Y' ");
		sql.append(" and c.createdBy = :createdBy ");
		sql.append(" order by creationDate desc ");
		params.put("createdBy", context.getUserId());
		List<IntairOrder> orders = this.findList(sql.toString(), params);
		if (orders != null && !orders.isEmpty()){
			return orders.get(0);
		}
		return null;
	}

	public IntairOrder getNewestOrder(ContextInfo context) {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from IntairOrder c where c.enabledFlag = 'Y' ");
		sql.append(" order by c.orderId desc ");
		List<IntairOrder> orders = this.findListByMax(sql.toString(), 1, params);
		if (orders != null && !orders.isEmpty()){
			return orders.get(0);
		}
		return null;
	}

}
